package breeze.pixel.weather.search_city.view;

import android.content.Intent;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.util.Log;
import android.util.Property;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.TextView;


import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.qweather.sdk.bean.base.Code;
import com.qweather.sdk.bean.base.Lang;
import com.qweather.sdk.bean.base.Range;
import com.qweather.sdk.bean.geo.GeoBean;
import com.qweather.sdk.view.QWeather;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.Set;

import breeze.pixel.weather.BaseView;
import breeze.pixel.weather.R;
import breeze.pixel.weather.adapter.AppWeatherManager;
import breeze.pixel.weather.adapter.IAppWeatherHelper;
import breeze.pixel.weather.adapter.weather.CityAdapter;
import breeze.pixel.weather.apps_util.views.mtextview.MTextView;
import breeze.pixel.weather.search_city.model.SearchCityAdapter;

public class SearchCityView extends BaseView implements ISearchCityView {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_city_search);
        init();
        initData();
    }

    private ListView listview;
    private MTextView none_textView;
    private View view_listview;
    private View view_textview;
    private View view_loading;
    private FrameLayout framelayout;
    private EditText edittext;

    @Override
    public void init() {
        view_listview = LayoutInflater.from(this).inflate(R.layout.view_listview, null);
        view_loading = LayoutInflater.from(this).inflate(R.layout.view_loading, null);
        listview = view_listview.findViewById(R.id.view_listview_listview);
        view_textview = LayoutInflater.from(this).inflate(R.layout.view_none_things, null);
        none_textView = view_textview.findViewById(R.id.view_none_textview);
        framelayout = findViewById(R.id.activitycitysearchFrameLayout1);
        edittext = findViewById(R.id.activitycitysearchEditText1);
        edittext.setOnEditorActionListener((v, actionId, event) -> {
            if (actionId == EditorInfo.IME_ACTION_DONE) {
                String text = edittext.getText().toString();
                if ("".equals(text)) {
                    toast("请输入城市名称");
                } else {
                    searchCity(text);
                }
            }
            return true;
        });
    }

    @Override
    public void initData() {

    }

    public void back(View view) {
        finish();
    }

    public void search(View view) {
        String text = edittext.getText().toString();
        if ("".equals(text)) {
            toast("请输入城市名称");
        } else {
            searchCity(text);
        }
    }


    @Override
    public void turnToListView() {
        runOnUiThread(() -> {
            framelayout.removeAllViews();
            framelayout.addView(view_listview);
        });
    }

    @Override
    public void turnToNoThings(String message) {
        runOnUiThread(() -> {
            framelayout.removeAllViews();
            none_textView.setText(message);
            framelayout.addView(view_textview);
        });
    }

    @Override
    public void turnToLoading() {
        runOnUiThread(() -> {
            framelayout.removeAllViews();
            framelayout.addView(view_loading);
        });
    }

    SearchCityAdapter adapter;
    List<CityAdapter.LocationBean> seachCity = new ArrayList<>();

    @Override
    public void searchCity(String cityName) {
        seachCity.clear();
        turnToLoading();
        IAppWeatherHelper helper = AppWeatherManager.getHelper();
        helper.getCityInfo(cityName, new IAppWeatherHelper.CityQueryResponse() {
            @Override
            public void success(CityAdapter data) {
                    setResultData(data);
            }

            @Override
            public void fail(String err) {
                turnToNoThings(err);
            }
        });
    }

    @Override
    public void setResultData(CityAdapter data) {
        this.seachCity = data.getBeans();
        this.adapter = new SearchCityAdapter(SearchCityView.this, this.seachCity);
        if (!seachCity.isEmpty()) {
            this.adapter.notifyDataSetChanged();
            this.listview.setAdapter(adapter);
            turnToListView();
        } else {
            turnToNoThings("什么都没有噢");
        }
        this.listview.setOnItemClickListener((parent, view, position, id) -> {
            CityAdapter.LocationBean bean = seachCity.get(position);
            String cityCode = bean.getId();
            Intent intent = new Intent();
            intent.putExtra("cityCode", cityCode);
            SearchCityView.this.setResult(1, intent);
            finish();
        });
    }
}